package com.haohan.stock.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.JdkIdGenerator;

import com.haohan.stock.dao.StockDAO;
import com.haohan.stock.domain.Stock30;
import com.haohan.util.CodeList;
import com.haohan.util.DataUtil;

/**
 * 奖项数据操作类
 * 
 * @author Administrator
 *
 */
@Service
public class StockService {

	@Autowired
	private StockDAO stockDAO;

	public String saveinit() {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			List<Stock30> list = DataUtil.getStockday_data_list10000(a[i]);
			if (list == null) {
				continue;
			}
			for (Iterator<Stock30> iterator = list.iterator(); iterator.hasNext();) {
				Stock30 stock30 = (Stock30) iterator.next();
				stock30.setId(new JdkIdGenerator().generateId().toString());
				stock30.setCode(a[i]);
				stockDAO.save(stock30);
			}
		}
		return "初始化成功!";
	}

	public String saveinit8() {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			List<Stock30> list = DataUtil.getStockday_data_list8(a[i]);
			if (list == null) {
				continue;
			}
			for (Iterator<Stock30> iterator = list.iterator(); iterator.hasNext();) {
				Stock30 stock30 = (Stock30) iterator.next();
				stock30.setId(new JdkIdGenerator().generateId().toString());
				stock30.setCode(a[i]);
				stockDAO.save(stock30);
			}
		}
		return "初始化成功!";
	}

	/**
	 * 计算数据的均量和均线并更新
	 * 
	 * @return
	 */
	public String calcea() {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			List<Stock30> list = stockDAO.select(a[i]).getContent();
			if (list == null) {
				continue;
			}
			calcPrice(list, 5);
			calcPrice(list, 10);
			calcPrice(list, 20);
			calcPrice(list, 30);
			for (Iterator<Stock30> iterator = list.iterator(); iterator.hasNext();) {
				Stock30 stock30 = (Stock30) iterator.next();
				stockDAO.save(stock30);
			}
		}
		return "初始化成功!";
	}
	
	/**
	 * 计算集合里param均值
	 * @param list
	 * @param param
	 * @return
	 */
	public void calcPrice(List<Stock30> list,int param) {
		Map<String, Double> map = new HashMap<String, Double>();
		for (int i = 0; i < list.size(); i++) {
			double pricema = 0d;
			if ((i + param-1) >= list.size()) {
				break;
			}
			for (int j = i; j < i + param; j++) {
				pricema += list.get(j).getClose();
			}
			map.put(list.get(i + param-1).getDay(), pricema / (param+0d));
		}
		for (Iterator<Stock30> iterator = list.iterator(); iterator.hasNext();) {
			Stock30 stock30 = (Stock30) iterator.next();
			if(map.get(stock30.getDay())==null){
				continue;
			}
			if(param==5){
				stock30.setPrice5(map.get(stock30.getDay()));
			}
			if(param==10){
				stock30.setPrice10(map.get(stock30.getDay()));
			}
			if(param==20){
				stock30.setPrice20(map.get(stock30.getDay()));
			}
			if(param==30){
				stock30.setPrice30(map.get(stock30.getDay()));
			}
		}
	}


	/**
	 * 回测股票的盈利点位
	 * 
	 * @param stockcode
	 * @return
	 */
	public String backTrace(String stockcode) {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			List<Stock30> list = DataUtil.getStockday_data_list8(a[i]);
			if (list == null) {
				continue;
			}
			for (Iterator<Stock30> iterator = list.iterator(); iterator.hasNext();) {
				Stock30 stock30 = (Stock30) iterator.next();
				stock30.setId(new JdkIdGenerator().generateId().toString());
				stock30.setCode(a[i]);
				stockDAO.save(stock30);
			}
		}
		return "初始化成功!";
	}
}
